package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDishes;
import com.itheima.domain.vo.SetmealVo;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishesService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishesService setmealDishesService;

    @Autowired
    private CategoryService categoryService;


    @Override
    public R<String> adminSetmealSave(SetmealVo setmealVo) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVo, setmeal);
        //类型:1菜品类型，2套餐类型
        setmeal.setType(2);
        //套餐排序
        setmeal.setSort(1L);
        //获取分类名称
        Category category = categoryService.getById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());
        // 调用存储方法
        boolean resultDish = save(setmeal);
        int size = setmealVo.getSetmealDishes().size();
        String setmealId = setmeal.getId();


        ArrayList<SetmealDishes> setmealDishesArrayList=new ArrayList<>();

        for (int i = 0; i < size; i++) {
            SetmealDishes setmealDishes=new SetmealDishes();
            BeanUtils.copyProperties(setmealVo.getSetmealDishes().get(i),setmealDishes);
            //把新增套餐的id添加给菜品id
            setmealDishes.setId(setmealId);
            setmealDishes.setSort(1L);
            setmealDishesArrayList.add(setmealDishes);
        }

        boolean resultAddSetmealDishes = setmealDishesService.saveBatch(setmealDishesArrayList);

        if (resultDish&&resultAddSetmealDishes) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @Override
    public R<Page<Setmeal>> adminSetmealPageselect(String name,Integer page,Integer pageSize){
        // 构造分页构造器
        Page<Setmeal> pageInfo =  new Page<>(page, pageSize);
        //System.out.println("登录id"+TokenHolder.getCurrentId());
        // 构造条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName, name);

        // 添加排序
        queryWrapper.orderByDesc(Setmeal::getSort);
        // 执行查询
        page(pageInfo,queryWrapper);
        return  R.success(pageInfo);
    }

    @Override
    public R<String> adminSetmealUpdate(SetmealVo setmealVo){

        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealVo,setmeal);
        boolean resultUpdateSetmeal = updateById(setmeal);

        String id = setmealVo.getId();
        setmealDishesService.removeById(id);
        int size = setmealVo.getSetmealDishes().size();
        ArrayList<SetmealDishes> setmealDishesArrayList=new ArrayList<>();
        for (int i = 0; i < size; i++) {
            SetmealDishes setmealDishes=new SetmealDishes();
            BeanUtils.copyProperties(setmealVo.getSetmealDishes().get(i),setmealDishes);
            setmealDishes.setSort(1L);
            setmealDishes.setId(id);
            setmealDishesArrayList.add(setmealDishes);
        }
        boolean resultUpdateSetmealDishes = setmealDishesService.saveOrUpdateBatch(setmealDishesArrayList);

        if (resultUpdateSetmeal&&resultUpdateSetmealDishes){
            return R.success("修改套餐成功");
        }
        return R.error("修改套餐失败");
    }

    @Override
    public R<SetmealVo> adminSetmealSelectById(String id){
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getId,id);
        Setmeal setmeal = getOne(lqw);

        LambdaQueryWrapper<SetmealDishes> lqwdf=new LambdaQueryWrapper<>();
        lqwdf.eq(SetmealDishes::getId,id);
        List<SetmealDishes> setmealDishes = setmealDishesService.list(lqwdf);

        SetmealVo setmealVo = new SetmealVo();
        BeanUtils.copyProperties(setmeal,setmealVo);
        BeanUtils.copyProperties(setmealDishes,setmealVo);


        return  R.success(setmealVo);
    }


    @Override
    public R<String> adminSetmealUpdateStatus( List<String> ids, Integer status){
        ArrayList<Setmeal> setmealArrayList=new ArrayList<>();
        for (String id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealArrayList.add(setmeal);
        }

        boolean resultUpdateSetmealList = updateBatchById(setmealArrayList);
        if (resultUpdateSetmealList){
            return R.success("操作成功");
        }

        return R.error("操作失败");

    }

    @Override
    public R<String> adminSetmealDelete(List<String> ids){
        boolean resultRemoveSetmeal = removeByIds(ids);
        boolean resultRemoveSetmealDishes = removeByIds(ids);
        if (resultRemoveSetmeal&&resultRemoveSetmealDishes){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }


    @Override
    public R<List<SetmealVo>> clientCategoryList( String categoryId, String name ){
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(categoryId),Setmeal::getCategoryId,categoryId)
                .like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        List<Setmeal> setmealList =list(lambdaQueryWrapper);

        List<String> idList = setmealList.stream().map(Setmeal::getId).collect(Collectors.toList());
        //通过菜品id集合获取到与菜品关联的口味集合....一个菜品可以有多个口味
        List<SetmealDishes> setmealDishesList = setmealDishesService.listByIds(idList);
        Map<String,ArrayList<SetmealDishes>> map=new HashMap<>(16);

        for (SetmealDishes setmealDishes : setmealDishesList) {
            String id = setmealDishes.getId();
            ArrayList<SetmealDishes> setmealDishesArrayList=new ArrayList<>();
            boolean result = map.containsKey(id);
            if (result){
                ArrayList<SetmealDishes> setmealDishes1 = map.get(id);
                setmealDishes1.add(setmealDishes);
                map.put(id, setmealDishes1);
                //跳出这次循环
                continue;
            }
            setmealDishesArrayList.add(setmealDishes);
            map.put(id, setmealDishesArrayList);
        }

        List<SetmealVo> setmealVoList = setmealList.stream().map(setmeal -> {
            SetmealVo setmealVo = new SetmealVo();
            setmealVo.setSetmealDishes(map.get(setmeal.getId()));
            BeanUtils.copyProperties(setmeal, setmealVo);
            return setmealVo;
        }).collect(Collectors.toList());


        return R.success(setmealVoList);
    }


    @Override
    public R<List<SetmealDishes>> clientSelectSetmealDishesById(String id){
        LambdaQueryWrapper<SetmealDishes> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SetmealDishes::getId,id);
        List<SetmealDishes> list = setmealDishesService.list(lqw);
        return  R.success(list);
    }


}
